<template>
  <div class="container">
    <el-row type="flex" justify="center">
      <el-col :span="23">
        <el-card class="box-card">
          <div class="header">
            <el-form ref="ruleForm" label-width="100px" class="demo-ruleForm">
              <el-row>
                <el-col :span="6">
                  <!-- 目录 -->
                  <el-form-item label="目录名称">
                    <el-input v-model="directoryName"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <!-- 状态 -->
                  <el-form-item label="状态">
                    <el-select placeholder="请选择活动区域" v-model="state">
                      <el-option label="启用" value="1"></el-option>
                      <el-option label="禁用" value="0"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <!-- 清楚收索 -->
                  &nbsp;
                  <el-button>清除</el-button>
                  <el-button type="primary" @click="search">搜索</el-button>
                </el-col>
                <el-col :span="6" style="text-align: right">
                  <el-button
                    type="success"
                    icon="el-icon-edit"
                    @click="openDialog()"
                    >新增标签</el-button
                  >
                </el-col>
              </el-row>
            </el-form>
          </div>
          <!-- 显示多少条数据 -->
          <div class="message">
            <el-row>
              <el-col>
                <el-button
                  type="primary"
                  icon="el-icon-message"
                  style="width: 100%; text-align: left"
                  >数据一共{{ counts }}条</el-button
                >
              </el-col>
            </el-row>
          </div>
          <!-- 表格 -->
          <div class="tabell">
            <el-table :data="tableData" stripe style="width: 100%">
              <el-table-column type="index" label="序号" width="50">
              </el-table-column>
              <el-table-column prop="subjectName" label="所属学科" width="150">
              </el-table-column>
              <el-table-column
                prop="directoryName"
                label="目录名称"
                width="180"
              >
              </el-table-column>
              <el-table-column prop="username" label="创建者" width="180">
              </el-table-column>
              <el-table-column prop="addDate" label="创建日期" width="180">
              </el-table-column>
              <el-table-column prop="totals" label="面试题数量" width="180">
              </el-table-column>
              <el-table-column prop="state" label="状态" width="180">
                <template v-slot="{ row }">
                  <el-tag v-if="row.state == 1" type="success">启用</el-tag>
                  <el-tag v-else type="danger">禁用</el-tag>
                </template>
              </el-table-column>
              <!-- 操作 -->
              <el-table-column label="操作">
                <template v-slot="{ row }">
                  <el-button type="text" @click="handleEdit(row)">
                    <span v-if="row.state === 1">禁用</span>
                    <span v-else>启用</span>
                  </el-button>

                  <el-button
                    type="text"
                    @click="handleEditDia(row)"
                    :disabled="row.state === 1"
                    >修改</el-button
                  >

                  <el-button
                    type="text"
                    @click="handleDelete(row)"
                    :disabled="row.state === 1"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </div>
          <div class="dia">
            <el-dialog
              title="新增目录"
              :visible.sync="dialogVisible"
              width="30%"
              @close="handleClose()"
            >
              <span>
                <el-form label-width="100px" class="demo-ruleForm">
                  <!-- 所属学科 -->
                  <el-form-item label="所属学科">
                    <!-- 写到这里了 给select绑定moodel -->
                    <el-select placeholder="请选择活动区域" v-model="MlId">
                      <el-option
                        v-for="v in subjectList"
                        :label="v.label"
                        :value="v.value"
                        :key="v.value"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                  <!-- 目录名称 -->
                  <el-form-item label="目录名称">
                    <el-input v-model="Mlname"></el-input>
                  </el-form-item>
                </el-form>
              </span>
              <span slot="footer" class="dialog-footer">
                <el-button @click="cancelDia()">取 消</el-button>
                <el-button type="primary" @click="closeDialog">确 定</el-button>
              </span>
            </el-dialog>
          </div>
          <div class="pagination">
            <div style="float: right;padding: 20px;">
              <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="page"
                :page-sizes="[5, 10, 20, 50]"
                :page-size="pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="counts"
              >
              </el-pagination>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {
  list,
  add,
  changeState,
  update,
  remove
} from '@/api/hmmm/directorys.js'
import { simple } from '@/api/hmmm/subjects.js'

export default {
  data () {
    return {
      tableData: [],
      page: 1,
      pagesize: 10,
      counts: 0,
      directoryName: '',
      state: '',
      dialogVisible: false,
      subjectList: [],
      Mlname: '',
      MlId: '',
      edit: '',
      id: ''
    }
  },
  created () {
    this.getList({ page: this.page, pagesize: this.pagesize })
    this.getSubject()
  },
  methods: {
    handleCurrentChange (value) {
      console.log(value, 'current')
      this.page = value
      this.getList({ page: this.page, pagesize: this.pagesize })
    },
    handleSizeChange (val) {
      console.log(val, 'size')
      this.pagesize = val
      this.page = 1
      this.getList({ page: this.page, pagesize: this.pagesize })
    },
    async getList (data) {
      const res = await list(data)
      console.log(res)
      this.tableData = res.data.items
      this.counts = res.data.counts
      console.log(this.tableData)
    },
    async getSubject () {
      const res = await simple()
      console.log(res)
      this.subjectList = res.data
    },
    async search () {
      const res = await this.getList({
        page: this.page,
        pagesize: this.pagesize,
        directoryName: this.directoryName,
        state: this.state
      })
      console.log(res)
    },
    async handleEdit (row) {
      console.log(row)
      if (row.state === 1) {
        const res = await changeState({ id: row.id, state: 0 })
        console.log(res)
      }
      if (row.state === 0) {
        const res = await changeState({ id: row.id, state: 1 })
        console.log(res)
      }
      this.getList({ page: this.page, pagesize: this.pagesize })
      // changeState({id:})
    },
    handleEditDia (row) {
      console.log(row)
      this.Mlname = row.directoryName
      this.MlId = row.subjectID
      this.id = row.id
      this.edit = 'xiugai'
      this.dialogVisible = true
    },
    async closeDialog () {
      if (this.edit === 'xiugai') {
        console.log('xiugai')
        const res = await update({
          id: this.id,
          subjectID: this.MlId,
          directoryName: this.Mlname
        })
        console.log(res)
        this.$message({
          message: '修改成功',
          type: 'success'
        })
      }

      if (this.edit === 'add') {
        const res = await add({
          subjectID: this.MlId,
          directoryName: this.Mlname
        })
        console.log(res)
        this.$message({
          message: '添加成功',
          type: 'success'
        })
      }

      this.dialogVisible = false
      this.getList({ page: this.page, pagesize: this.pagesize })
    },
    openDialog () {
      this.edit = 'add'
      this.dialogVisible = true
    },
    cancelDia () {
      this.edit = ''
      this.Mlname = ''
      this.MlId = ''
      this.dialogVisible = false
    },
    handleClose () {
      this.edit = ''
      this.Mlname = ''
      this.MlId = ''
      console.log('xxxx')
    },
    handleDelete (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await remove({ id: row.id })
        console.log(res)
        this.getList({ page: this.page, pagesize: this.pagesize })
        console.log('我确定删除')
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      })
    }
  }
}
</script>

<style scoped lang='less'>

</style>
